<?php
class treePages extends module {
	public $title = "Editable tree pages";
	
	public function run() {
		$pageKey = (is_natural($this->engine->getUrlParam(0)))?$this->engine->getUrlParam(0):0;
		
		$pageData = $this->sql->getRowFromDB("SELECT name, title, keywords, description, content FROM tree_pages WHERE id='".$pageKey."' AND published='yes'");
		if ($pageData) {
				$this->xml->currentPageTitle = $pageData['title'];
				$this->xml->currentMetaKeywords = $pageData['keywords'];
				$this->xml->currentMetaDescription = $pageData['description'];
				$this->xml->name = $pageData['name'];
				$this->xml->content = $pageData['content'];
		}
		
		$this->engine->moduleRun("treePages", "treeMenu", array($pageKey,1), false, $this->xml);
	}

	public function treeMenu($root=0, $maxLevel=3, $xml=null) {
		if (!isset($this->level)) $this->level = 1;
		
		if (!$xml) $xml = $this->xml;
		$listData = $this->sql->getArrayFromDB("SELECT id, name FROM tree_pages WHERE parent_id='".$root."' AND published='yes' ORDER BY order_key");
		for ($i=0; $i<count($listData); $i++) {
			$xml->item[$i] = "";
			$xml->item[$i]['id'] = $listData[$i]['id'];
			$xml->item[$i]['level'] = $this->level;
			$xml->item[$i]['name'] = $listData[$i]['name'];
			$this->level++;
			if ($maxLevel>=$this->level) $this->treeMenu($listData[$i]['id'], $maxLevel, $xml->item[$i]);
			$this->level--;
		}
	}
}
?>